﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Text.RegularExpressions

Partial Public Class chuyenTV
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Public Shared Function StripDiacritics(ByVal accented As String) As String
        Dim regex As New Regex("\p{IsCombiningDiacriticalMarks}+")

        Dim strFormD As String = accented.Normalize(System.Text.NormalizationForm.FormD)
        Return regex.Replace(strFormD, [String].Empty).Replace("đ"c, "d"c).Replace("Đ"c, "D"c)
    End Function

    'Private Sub btnSanpham_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSanpham.Click

    '    Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
    '    Dim sSql As String, sqlUpdate As String = ""
    '    Dim myDr As SqlDataReader
    '    Dim idsp, tensp As String

    '    sSql = "select idsanpham, tensanpham from sanpham where idloaisp in (1,2,3,4,21,27) order by idloaisp"
    '    myConn.Open()

    '    Dim myCommand As New SqlCommand(sSql, myConn)

    '    If myCommand.ExecuteScalar > 0 Then
    '        myCommand.CommandText = sSql
    '        myDr = myCommand.ExecuteReader
    '        While myDr.Read()
    '            idsp = myDr("idsanpham")
    '            tensp = Replace(Trim(myDr("tensanpham")), "  ", " ")

    '            sqlUpdate &= "update sanpham set tensprewrite = '" & LCase(Replace(StripDiacritics(tensp), " ", "-")) & "' where idsanpham = " & idsp & " "

    '        End While

    '        myDr.Close()
    '    End If

    '    Try
    '        myCommand.CommandText = sqlUpdate
    '        myCommand.ExecuteNonQuery()

    '    Catch ex As Exception
    '        Response.Write(ex.Message & "<br>")
    '    End Try

    '    myCommand.Dispose()
    '    myConn.Close()
    'End Sub


    Private Sub btnSanpham_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSanpham.Click

        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim sSql As String, sqlUpdate As String = ""
        Dim myDr As SqlDataReader
        Dim idsp, tensp As String

        sSql = "select idtintuc, tieude from tintuc where idloaitt in (1,2,4,9)"
        myConn.Open()

        Dim myCommand As New SqlCommand(sSql, myConn)

        If myCommand.ExecuteScalar > 0 Then
            myCommand.CommandText = sSql
            myDr = myCommand.ExecuteReader
            While myDr.Read()
                idsp = myDr("idtintuc")
                tensp = Replace(Trim(myDr("tieude")), "  ", " ")
                tensp = Replace(tensp, "   ", " ")
                tensp = Replace(tensp, " – ", " ")
                tensp = Replace(tensp, "?", "")
                tensp = Replace(tensp, ":", "")
                
                'Response.Write(LCase(Replace(StripDiacritics(tensp), " ", "-")) & "<br>")
                sqlUpdate &= "update tintuc set tieude_rewrite = '" & LCase(Replace(StripDiacritics(tensp), " ", "-")) & "' where idtintuc = " & idsp & " "

            End While

            myDr.Close()
        End If

        Try
            myCommand.CommandText = sqlUpdate
            myCommand.ExecuteNonQuery()

        Catch ex As Exception
            Response.Write(ex.Message & "<br>")
        End Try

        myCommand.Dispose()
        myConn.Close()
    End Sub

End Class